package com.blog.login.controller;

import com.blog.login.service.UserService;
import com.blog.utils.JWTUtils;
import com.blog.utils.ResponseResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.Map;

/**
 * @author admin
 * @version 1.0 <br/>
 * <p style="font-family:SimSun;font-weight:bold">时间:<br/>2021/12/8 19:43</p>
 *
 */
@RestController
@Slf4j
@CrossOrigin(origins = "*")
@RequestMapping("/user")
public class LoginController {
    @Autowired
    UserService userService;
    @PostMapping("/login")
    public ResponseResult userLogin(String username,String password){
       Map<String,Object> login = userService.login(username, password);
        Object message = login.get("message");
        if (message !=null){
            log.info("用户："+username+" 登录失败");
           return ResponseResult.error().message(message.toString());
       }
        log.info("用户："+username+" 已登录");
        return ResponseResult.ok().message(" 登录成功").data(login);
    }

    @GetMapping("/info")
    public ResponseResult getInfo(@RequestHeader("token") String token){
        Map<String, Object> info = JWTUtils.getInfo(token);
        if(!ObjectUtils.isEmpty(info))
        return ResponseResult.ok().data(info);
        return ResponseResult.error().message("获取用户信息失败");
    }
    @PostMapping("/logout")
    public ResponseResult LogOutFromLogin(String username){
        log.info("用户："+username+" 已退出登录");
        return ResponseResult.ok().message("用户已退出登录");
    }
}
